package repository;

import java.util.*;
import dto.*;
import java.sql.*;


public class OrderRepository {
      private DataBase db=null; 
      private PreparedStatement sqlGetIdStatus;
      private PreparedStatement sqlCreate;
      private PreparedStatement sqlUpdateStatus;
      private PreparedStatement sqlGetLastCreatedId;
      private PreparedStatement sqlGetOrdersForConfirm;
      
    
      public OrderRepository()
      {
           try
       {
           db=new DataBase();
       }
       catch(Exception e){}
      }
      
      public int GetLastCreatedId()
      {
          try
          {
              sqlGetLastCreatedId=db.connection.prepareStatement("SELECT id FROM Orders ORDER BY id DESC");
              ResultSet resultSet=sqlGetLastCreatedId.executeQuery();
              if (!resultSet.next())
              {       
               return -1;
              }
              return resultSet.getInt(1);
          }
          catch(Exception e)
          {
              return -1;
          }
      }
      
      public boolean UpdateSatusToCreated()
      {
          try
          {
              sqlUpdateStatus=db.connection.prepareStatement("UPDATE Orders SET status = ? WHERE status = ?");
              sqlUpdateStatus.setInt(1, 1);
              sqlUpdateStatus.setInt(2, 0);
              int result=sqlUpdateStatus.executeUpdate();
              if (result==0)
              {
               db.connection.rollback();
               return false;
              }
              return true;
          }
          catch(Exception e)
          {
              return false;
          }
      }
      
      public boolean UpdateSatusToConfirmed(int orderId)
      {
          try
          {
              sqlUpdateStatus=db.connection.prepareStatement("UPDATE Orders SET status = ? WHERE status = ? and id = ?");
              sqlUpdateStatus.setInt(1, 2);
              sqlUpdateStatus.setInt(2, 1);
              sqlUpdateStatus.setInt(3, orderId);
              int result=sqlUpdateStatus.executeUpdate();
              if (result==0)
              {
               db.connection.rollback();
               return false;
              }
              return true;
          }
          catch(Exception e)
          {
              return false;
          }
      }
      
      public boolean Create(Order newOrder)
      {
          try
          {
              sqlCreate=db.connection.prepareStatement("INSERT INTO Orders (userId, status, totalPrice, orderName) VALUES (?, 0, ?, ?)");
              sqlCreate.setInt(1, newOrder.userId);
              sqlCreate.setDouble(2, newOrder.totalPrice);
              sqlCreate.setString(3, newOrder.orderName);
              
              int result=sqlCreate.executeUpdate();
              if (result==0)
              {
               db.connection.rollback();
               return false;
              }
              return true;
           }
          catch (Exception e)
          {
              return false;
          }
       }
      
      
      public int GetIdByStatus(int statusId)
      {
       try
       {
       sqlGetIdStatus=db.connection.prepareStatement("SELECT id FROM Orders where status = ?");
       sqlGetIdStatus.setInt(1, statusId);
       ResultSet resultSet=sqlGetIdStatus.executeQuery();
          if (!resultSet.next())
          {       
           return -1;
          }
       return resultSet.getInt(1);
       }
        catch(Exception e)
        {
           return -1;
        }
      }
      
    public List<ExtendedOrder> GetOrdersForConfirm()
    {
        try
        {
            sqlGetOrdersForConfirm=db.connection.prepareStatement("SELECT o.id, o.orderName, o.totalPrice, u.userName, u.surname FROM Orders as o, Users as u WHERE o.status = 2 AND o.userId = u.id");
            ResultSet resultSet=sqlGetOrdersForConfirm.executeQuery();
            
            List<ExtendedOrder> oList=new ArrayList<ExtendedOrder>();
            while (resultSet.next())
            {       
                int gotOrderId = resultSet.getInt(1);
                String gotOrderName =resultSet.getString(2);
                double gotTotalPrice =resultSet.getDouble(3);
                String gotUserName=resultSet.getString(4);
                String gotSurname=resultSet.getString(5);
                ExtendedOrder gotOrder = new ExtendedOrder(gotOrderId,gotOrderName,gotTotalPrice,gotUserName,gotSurname);
                oList.add(gotOrder);
            }
            return oList;            
        }
        catch(Exception e)
        {
            return null;
        }
    }
    
    public boolean UpdateSatusToConfirmedByAdmin(String ids)
      {
          try
          {
              sqlUpdateStatus=db.connection.prepareStatement("UPDATE Orders SET status = 3 WHERE id = ?");
              String [] strIds = ids.split(",");
                
                for (int i=0;i<strIds.length;i++)
                {
                   sqlUpdateStatus.setInt(1, Integer.parseInt(strIds[i])); 
                   int result=sqlUpdateStatus.executeUpdate();              
                   if (result==0)
                   {
                   db.connection.rollback();
                   return false;
                   }
                }
              return true;
          }
          catch(Exception e)
          {
              return false;
          }
      }
}
